package edu.lingnan.rili.controller;


import edu.lingnan.rili.service.AwardedService;
import edu.lingnan.rili.utils.DownloadUtils;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.AwardedVo;
import edu.lingnan.rili.vo.MarksVo;
import edu.lingnan.rili.vo.PagingQueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import edu.lingnan.rili.service.AwardedService;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.PagingQuery;
import edu.lingnan.rili.vo.PagingQueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import static edu.lingnan.rili.controller.AdministratorController.isAdminLogin;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/awarded")
@Api(tags = "其他比赛加分控制器")
public class AwardedController {

    @Autowired
    private AwardedService awardedService;

    @ApiOperation("分页查询所有已经公示的加分数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前页数", required=true, paramType="query"),
            @ApiImplicitParam(name="pageSize",value="每页条数", required=true, paramType="query")
    })
    @GetMapping("/announced-page")
    public Result<PagingQueryResult<AwardedVo>> getByAnnouncedAndPage(@RequestParam("currentPage") String currentPage,
                                                                      @RequestParam("pageSize") String pageSize,
                                                                      @ApiIgnore HttpSession session) {
        // 判断参数是否为空
        if ("".equals(currentPage) || "".equals(pageSize) || StringUtils.isEmpty(currentPage) ||  StringUtils.isEmpty(pageSize)) {
            return new Result<>(400, "请输入有效数据");
        }

        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        PagingQueryResult<AwardedVo> allAnnounced = awardedService.selectByAnnouncedAndPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        if (ObjectUtils.isEmpty(allAnnounced) || allAnnounced.getPageTotal() <= 0) {
            return new Result<>(201, "查询失败，未找到有效数据");
        }

        Result<PagingQueryResult<AwardedVo>> result = new Result<>(200, "查询成功");
        result.setData(allAnnounced);
        System.out.println(allAnnounced);
        System.out.println("+++++++++++++++++++");

        return result;
    }

    @ApiOperation("导出其他比赛加分表中已公示数据")
    @PostMapping("/download")
    public Result<Null> downloadExcel(@ApiIgnore HttpServletResponse response,
                                      @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        //构建写入到Excel中的数据
        List<AwardedVo> allAnnounced = awardedService.selectByAnnounced();
        System.out.println(allAnnounced);
        try {
            DownloadUtils.download(response, "其他比赛加分公示栏", allAnnounced, AwardedVo.class);
        } catch (IOException e) {
            return new Result<>(201, "表格导出错误，请联系管理人员");
        }

        return new Result<>(200, "表格导出成功");
    }

    // @PostMapping("/not-reviewed")
    @ApiOperation("查询其他比赛加分待审核数据")
    public Result<List<AwardedVo>> selectAllNotReviewed(@ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<List<AwardedVo>> result = new Result<>(201, "查询失败! 未找到有效数据!");
        List<AwardedVo> awardedVos = awardedService.selectAllNotReviewed();
        if (awardedVos != null && !ObjectUtils.isEmpty(awardedVos)) {
            result.setResultCode(200);
            result.setMessage("查询成功，共" + awardedVos.size() + "条数据待审核");
            result.setData(awardedVos);
        }
        return result;
    }

    @ApiOperation("分页查询其他比赛加分待审核数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前页数", required=true, paramType="query"),
            @ApiImplicitParam(name="pageSize",value="每页条数", required=true, paramType="query")
    })
    @GetMapping("/not-reviewed-page")
    public Result<PagingQueryResult<AwardedVo>> selectAllNotReviewedByPage(@RequestParam("currentPage") String currentPage,
                                                                           @RequestParam("pageSize") String pageSize,
                                                                           @ApiIgnore HttpSession session) {
        if ("".equals(currentPage) || "".equals(pageSize) || StringUtils.isEmpty(currentPage) ||  StringUtils.isEmpty(pageSize)) {
            return new Result<>(400, "请输入有效数据");
        }

        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        PagingQueryResult<AwardedVo> allNotRevieweds = awardedService.selectAllNotReviewedByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        if (allNotRevieweds == null || ObjectUtils.isEmpty(allNotRevieweds) || allNotRevieweds.getPageTotal() <= 0) {
            return new Result<>(201, "查询失败，未找到有效数据");
        }

        Result<PagingQueryResult<AwardedVo>> result = new Result<>(200, "查询成功");
        result.setData(allNotRevieweds);

        return result;
    }

    @PostMapping("/audit")
    @ApiOperation("审核其他比赛加分数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadingId", value = "唯一标识", required = true, paramType = "query"),
            @ApiImplicitParam(name = "stateId", value = "审核情况标识，0 驳回，1通过，-1未审核，默认-1", required = true, paramType = "query"),
            @ApiImplicitParam(name = "statueCause", value = "驳回原因", required = false, paramType = "query")
    })
    public Result<Boolean> audit(@RequestParam("uploadingId") String uploadingId,
                                 @RequestParam("stateId") String stateId,
                                 @RequestParam("statueCause") String stateCause,
                                 @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }

        Boolean isAudit = awardedService.audit(uploadingId, stateId, stateCause);
        Result<Boolean> result = new Result<>(201, "审核操作失败！");
        if (isAudit) {
            result.setResultCode(200);
            result.setMessage("审核操作成功！");
        }
        result.setData(isAudit);
        return result;
    }

    /**
     * 这是一个其他比赛公示分页查询的方法
     *
     * @param page 查询的页数（第几页）
     * @param rows 一页显示的信息条数
     * @return
     */
    @GetMapping("/publicityOfOtherCompetitions")
    public Result<PagingQueryResult<PagingQuery>> publicityOfOtherCompetitions(int page, int rows) {
        return awardedService.publicityOfOtherCompetitions(page, rows);
    }

    /**
     * 这是一个其他加分公示分页查询的方法
     *
     * @param page 查询的页数（第几页）
     * @param rows 一页显示的信息条数
     * @return
     */
    @GetMapping("/publicityOfExtraPoints")
    public Result<PagingQueryResult<PagingQuery>> publicityOfExtraPoints(int page, int rows) {
        return awardedService.publicityOfExtraPoints(page, rows);
    }


    /**
     * 这是一个其他比赛公示页面的模糊查询
     *
     * @param keywords 关键字
     * @return
     */
    @GetMapping("/queryOfOtherCompetitionPublicityKeywords")
    public Result<PagingQueryResult<PagingQuery>> queryOfOtherCompetitionPublicityKeywords
                    (String keywords, int page, int rows) {
        return awardedService.queryOfOtherCompetitionPublicityKeywords(keywords,page,rows);
    }

    /**
     * 这是一个其他加分公示页面的模糊查询
     *
     * @param keywords 关键字
     * @return
     */
    @GetMapping("/fuzzyQueryOfOtherBonusPublicityPages")
    public Result<PagingQueryResult<PagingQuery>> fuzzyQueryOfOtherBonusPublicityPages
    (String keywords, int page, int rows) {
        return awardedService.fuzzyQueryOfOtherBonusPublicityPages(keywords,page,rows);
    }
}

